EMMA Coverage Report (generated Tue May 03 18:38:32 CEST 2016)
[all classes][kdk.android.simplydo]

COVERAGE SUMMARY FOR SOURCE FILE [SettingsActivity.java]

nameclass, %method, %block, %line, %
SettingsActivity.java100% (1/1)100% (6/6)93%  (254/273)89%  (51/57)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class SettingsActivity100% (1/1)100% (6/6)93%  (254/273)89%  (51/57)
backupDbSelected (): void 100% (1/1)84%  (97/116)71%  (15/21)
SettingsActivity (): void 100% (1/1)100% (9/9)100% (2/2)
copyDb (File, File): void 100% (1/1)100% (60/60)100% (12/12)
fileCopy (File, File): void 100% (1/1)100% (34/34)100% (10/10)
onCreate (Bundle): void 100% (1/1)100% (14/14)100% (5/5)
onPreferenceTreeClick (PreferenceScreen, Preference): boolean 100% (1/1)100% (40/40)100% (7/7)

1/*
2 * Copyright (C) 2013 Keith Kildare
3 * 
4 * This file is part of SimplyDo.
5 * 
6 * SimplyDo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * 
11 * SimplyDo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 * 
16 * You should have received a copy of the GNU General Public License
17 * along with SimplyDo.  If not, see <http://www.gnu.org/licenses/>.
18 * 
19 */
20package kdk.android.simplydo;
21 
22import java.io.File;
23import java.io.FileInputStream;
24import java.io.FileOutputStream;
25import java.io.IOException;
26import java.text.DecimalFormat;
27import java.util.Calendar;
28 
29import android.content.Intent;
30import android.os.Bundle;
31import android.os.Environment;
32import android.preference.Preference;
33import android.preference.PreferenceActivity;
34import android.preference.PreferenceScreen;
35import android.util.Log;
36import android.widget.Toast;
37 
38public class SettingsActivity extends PreferenceActivity
39{
40    private DecimalFormat twoDigits = new DecimalFormat("00");
41    private File backupDirectory;
42    
43    @Override
44    protected void onCreate(Bundle savedInstanceState)
45    {
46        super.onCreate(savedInstanceState);
47        addPreferencesFromResource(R.xml.settings);
48        
49        // TODO offer list of backup locations as preference
50        // at least suggested location and somewhere on ext. sdcard
51        backupDirectory = new File(
52                Environment.getExternalStorageDirectory(), 
53                "/Android/data/kdk.android.simplydo/files/");
54    }
55 
56    @Override
57    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
58            Preference preference)
59    {
60        Log.v(L.TAG, "onPreferenceTreeClick() for key " + preference.getKey());
61        
62        if("backupDb".equals(preference.getKey()))        
63        {
64            // TODO dialog: This will backup you current SimplyDo database. You
65            // can access and manage these backups through mass storage 
66            // access to you device. Make backup now to the file ....
67            backupDbSelected();
68        }
69        else if("restoreDb".equals(preference.getKey()))        
70        {
71            Intent restoreActivity = new Intent(getBaseContext(), RestoreActivity.class);
72            startActivity(restoreActivity);
73 
74        }
75        
76        // the android docs give no clues as to what the returned bool does
77        return super.onPreferenceTreeClick(preferenceScreen, preference);
78    }
79    
80    
81    private void backupDbSelected()
82    {
83        Calendar cal = Calendar.getInstance();
84        int seconds = (cal.get(Calendar.HOUR_OF_DAY) * 60 + cal.get(Calendar.MINUTE)) * 60 + cal.get(Calendar.SECOND);
85        String filename = 
86            "SimplyDo_" + 
87            cal.get(Calendar.YEAR) + 
88            twoDigits.format(cal.get(Calendar.MONTH) + 1) + 
89            twoDigits.format(cal.get(Calendar.DAY_OF_MONTH)) + "_" + 
90            seconds + ".simplydo";
91        
92        String state = Environment.getExternalStorageState();
93        
94        final File dbFile = getDatabasePath(DataManager.DATABASE_NAME);
95 
96        if (Environment.MEDIA_MOUNTED.equals(state)) 
97        {
98            // We can read and write the media
99            backupDirectory.mkdirs();
100            final File outFile = new File(backupDirectory, filename);
101            
102            Log.d(L.TAG, "Backing up to " + outFile.getAbsolutePath() + "\n" + dbFile.getAbsolutePath());
103            
104            copyDb(dbFile, outFile);
105        }
106        else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) 
107        {
108            // We can only read the media and we need to write
109            Toast t = Toast.makeText(this, R.string.settingsMediaReadOnly, Toast.LENGTH_LONG);
110            t.show();            
111        }
112        else 
113        {
114            // Something else is wrong.
115            Toast t = Toast.makeText(this, R.string.settingsMediaNoMount, Toast.LENGTH_LONG);
116            t.show();            
117        }
118    }
119    
120    
121    private void copyDb(File src, File dst)
122    {
123        try
124        {
125            SimplyDoActivity.getInstance().getDataVeiwer().flush();
126            
127            fileCopy(src, dst);
128            String backedUpTo = String.format(getString(R.string.settingsBackedUp), dst.getName());
129            Toast t = Toast.makeText(this, backedUpTo, Toast.LENGTH_SHORT);
130            t.show();
131        }
132        catch (Exception e)
133        {
134            Log.d(L.TAG, "Failed to copy files: " + e.getMessage(), e);
135            String backUpFailed = String.format(getString(R.string.settingsBackUpFailed), e.getMessage());
136            Toast t = Toast.makeText(this, backUpFailed, Toast.LENGTH_SHORT);
137            t.show();
138        }
139    }
140    
141    /**
142     * Copies the specified source file to the destination file 
143     * location.
144     * @param src Source file
145     * @param dst Destination file location
146     * @throws IOException On error.
147     */
148    public static void fileCopy(File src, File dst) throws IOException
149    {
150        FileInputStream srcStream = new FileInputStream(src);
151        FileOutputStream dstStream = new FileOutputStream(dst);
152        
153        byte[] buffer = new byte[1024];
154        
155        int bytesRead = srcStream.read(buffer);
156        while(bytesRead > 0)
157        {
158            dstStream.write(buffer, 0, bytesRead);
159            bytesRead = srcStream.read(buffer);
160        }
161        
162        dstStream.close();
163        srcStream.close();
164    }
165    
166 
167}

[all classes][kdk.android.simplydo]
EMMA 2.0.5312 (C) Vladimir Roubtsov